package com.design.adnroid.campussecondarytrading.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.design.adnroid.campussecondarytrading.database.DBOperate;
import com.design.adnroid.campussecondarytrading.entity.Person;

public class RegisterActivity extends AppCompatActivity {

    private Button btRegister;
    private EditText etName, etPassWord0, etPassWord1, etNiCheng;
    private CheckBox cbPWDdVisibility;
    private Spinner spinner;
    private DBOperate dbOperate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setTitle(getString(R.string.title_activity_register));

        btRegister = findViewById(R.id.register_register);
        etName = findViewById(R.id.register_name);
        etPassWord0 = findViewById(R.id.register_passWord0);
        etPassWord1 = findViewById(R.id.register_passWord1);
        etNiCheng = findViewById(R.id.register_nicheng);
        cbPWDdVisibility = findViewById(R.id.register_checkBox);
        spinner = findViewById(R.id.register_spinner);

        dbOperate = new DBOperate(this);

        cbPWDdVisibility.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //如果选中，显示密码
                    etPassWord0.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    etPassWord0.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

        spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                new String[]{"男", "女"}));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userRegister();
            }
        });
    }//onCreate()

    private void userRegister() {
        String userName = etName.getText().toString();
        String userPassword0 = etPassWord0.getText().toString();
        String userPassword1 = etPassWord1.getText().toString();
        String userNiCheng = etNiCheng.getText().toString();
        String userSex = spinner.getSelectedItem().toString();

        if (userName.isEmpty()) {
            etName.requestFocus();  //获取焦点
            etName.setError(getString(R.string.a_register_emptyError));
            return;
        }
        if (userPassword0.isEmpty()) {
            etPassWord0.requestFocus();
            etPassWord0.setError(getString(R.string.a_register_emptyError));
            return;
        }
        if (userNiCheng.isEmpty()) {
            etNiCheng.requestFocus();
            etNiCheng.setError(getString(R.string.a_register_emptyError));
            return;
        }

        if (userPassword0.equals(userPassword1)) {

            dbOperate.openDB();
            Toast.makeText(getApplicationContext(), userName, Toast.LENGTH_LONG).show();
            boolean flag = dbOperate.havePerson(userName);
            dbOperate.closeDb();
            if (!flag) {
                allowRegister(userName, userPassword1, userNiCheng, userSex);
                Toast.makeText(getApplicationContext(), "wu", Toast.LENGTH_LONG).show();
            } else {
                etName.requestFocus();
                etName.setError(getString(R.string.a_register_nameError));
            }
        } else {
            etPassWord1.requestFocus();
            etPassWord1.setError(getString(R.string.a_register_pwdError));
        }
    }

    //    注册用户,写入数据库,跳转到登录界面
    private void allowRegister(String userName, String userPassword, String userNiCheng, String userSex) {
        dbOperate.openDB();
        dbOperate.addPerson(new Person(userName, userPassword, userNiCheng, userSex));
        dbOperate.closeDb();

        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("userName", userName);
        intent.putExtra("userPassword", userPassword);
        startActivity(intent);
        finish();
    }


}
